//
//  HXDStatusRetweetView.swift
//  weibo
//
//  Created by huxuedong on 15/12/9.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit
import SnapKit

class HXDStatusRetweetView: UIView {
    
    private lazy var textLabel: HXDStatusTextLabel = HXDStatusTextLabel(textColor: UIColor.darkGrayColor(), textSize: textFontSize, maxWidth: kScreenW - 2 * margin)
    private lazy var retweetPictureView: HXDRetweetedPictureView = {
        let view = HXDRetweetedPictureView()
        return view
    }()
    
    var bottomConstraint: Constraint?
    var statusViewModel: HXDStatusViewModel? {
        didSet {
            textLabel.attributedText = statusViewModel?.retweetedStatusAttribute
            textLabel.specialString = statusViewModel?.retweetedStatusSpecialString
            //移除转发视图当前的底部约束
            self.bottomConstraint?.uninstall()
            //如果有图片数组并且长度大于0
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                retweetPictureView.pic_urls = pic_urls
                retweetPictureView.hidden = false
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetPictureView).offset(margin).constraint
                })
            } else {
                retweetPictureView.hidden = true
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(textLabel).offset(margin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(textLabel)
        addSubview(retweetPictureView)
        textLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(margin)
            make.trailing.equalTo(self).offset(-margin)
            make.top.equalTo(self).offset(margin)
        }
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(margin)
        }
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(retweetPictureView).offset(margin).constraint
        }
    }

}
